﻿Imports System.IO
Imports X360


Module FTPFunctions
    Friend ftp As New Chilkat.Ftp2
    Friend success, Passive As Boolean
    Friend pathname, HostName, UserName, Password As String
    Friend ConnectTimeout As Integer
    Friend Profilepathstring As Object
    Friend HDD, MU As Boolean

    Friend imageListLarge As New ImageList()

    ' When using events, the Chilkat.Ftp2 object must be a class member or global variable.
    Dim WithEvents ftp2 As New Chilkat.Ftp2

    ' Update the progress bar control with the percent done.
    Private Sub ftp2_OnFtpPercentDone(ByVal sender As Object, ByVal args As Chilkat.FtpPercentDoneEventArgs) Handles ftp2.OnFtpPercentDone

        FTPProgress.ProgressBar1.Value = args.PercentDone

    End Sub


    Friend Sub FTPOpen()

        ftp.Hostname = HostName
        ftp.Username = UserName
        ftp.Password = Password
        ftp.Passive = Passive
        ftp.ConnectTimeout = 10
        ftp.KeepSessionLog = True

        success = ftp.UnlockComponent(Main.ChilkatKey)
        If (Not success) Then
            MessageBox.Show(ftp.LastErrorText)
            Exit Sub
        End If

        If HostName = "" Or UserName = "" Or Password = "" Then
            MessageBox.Show("Please set the FTP options via the settings menu first", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        success = ftp.Connect()
        If (success <> True) Then
            MessageBox.Show("Connction failed. Make sure you have set the IP address correctly and loaded a FTP server on the Xbox 360", "Connect failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim i As Integer
        Dim n As Long
        ftp.ChangeRemoteDir("/hdd1/Content/")
        n = ftp.NumFilesAndDirs
        If (n < 0) Then
            MessageBox.Show(ftp.LastErrorText)
            Exit Sub
        End If

        Dim count As Integer = 0
        If (n > 0) Then
            For i = 0 To n - 1
                If ftp.GetFilename(i).StartsWith("E") Then

                    Dim profilename As String = ftp.GetFilename(i)

                    Dim dirHDD As String = Application.StartupPath & "\FTP\HDD1\" & ftp.GetFilename(i) & "\FFFE07D1\00010000\"

                    Dim remotedirHDD As String = "/hdd1/Content/" & ftp.GetFilename(i) & "/FFFE07D1/00010000/" ' & ftp.GetFilename(i)


                    'success = ftp.AsyncGetFileStart(remotedirHDD & ftp.GetFilename(i), dirHDD)
                    'FTPProgress.Show()

                    'Do Until ftp.AsyncFinished = True
                    '    FTPProgress.SpeedLabel.Text = ftp.AsyncBytesReceived
                    'Loop
                    success = ftp.ChangeRemoteDir(remotedirHDD & "/" & profilename)

                    If (success <> True) Then
                        MsgBox(ftp.LastErrorText)
                        Exit Sub
                    Else

                        My.Computer.FileSystem.CreateDirectory(dirHDD)
                        ftp.ChangeRemoteDir(remotedirHDD)
                        success = ftp.DownloadTree(dirHDD)

                        If (success <> True) Then
                            MessageBox.Show(ftp.LastErrorText)
                            'Exit Sub
                        End If

                        Dim gamertag As String
                        Dim GamerPic As Image
                        Profilepathstring = dirHDD & profilename
                        Dim xPackage As New X360.STFS.STFSPackage(Profilepathstring.ToString, Nothing)
                        Dim a As X360.STFS.ProfilePackage = New X360.STFS.ProfilePackage(xPackage)

                        GamerPic = a.Header.PackageImage

                        gamertag = a.UserFile.GetGamertag()
                        SelectProfile2.ListView1.Items.Add(gamertag, count)
                        SelectProfile2.ListView1.Items(count).SubItems.Add(remotedirHDD & profilename)

                        imageListLarge.Images.Add(GamerPic)

                        a.CloseIO()

                        ftp.ChangeRemoteDir("/hdd1/Content/")

                        'GetHddProfileList(i)
                        count += 1
                    End If
                End If

            Next

        End If

        'Dim mucount As Integer = 0
        'Do Until mucount = 2
        Dim mustring As String = "/OnBoardMU/"
        'success = ftp.ChangeRemoteDir("/MemUnitInt/Content/")
        'If success = True Then
        '    mustring = "/MemUnitInt/"
        'Else
        '    success = ftp.ChangeRemoteDir("/OnBoardMU/Content/")
        '    If success = True Then
        '        mustring = "/OnBoardMU/"
        '    End If
        'End If

        success = ftp.ChangeRemoteDir(mustring & "Content/")

        If success = True Then
            Dim nu As Long
            nu = ftp.NumFilesAndDirs
            If (n < 0) Then
                'MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            Else

                If (nu > 0) Then
                    For i = 0 To nu - 1
                        If ftp.GetFilename(i).StartsWith("E") Then

                            Dim profilename As String = ftp.GetFilename(i)


                            Dim dirMU As String = Application.StartupPath & "\FTP\OnBoardMU\" & ftp.GetFilename(i) & "\FFFE07D1\00010000\"
                            Dim remotedirmu As String = mustring & "Content/" & ftp.GetFilename(i) & "/FFFE07D1/00010000/" ' & ftp.GetFilename(i)

                            success = ftp.ChangeRemoteDir(remotedirmu & "/" & profilename)
                            If (success <> True) Then
                            Else

                                My.Computer.FileSystem.CreateDirectory(dirMU)
                                ftp.ChangeRemoteDir(remotedirmu)
                                success = ftp.DownloadTree(dirMU)

                                If (success <> True) Then
                                    MessageBox.Show(ftp.LastErrorText)
                                    'Exit For
                                End If

                                Dim gamertag As String
                                Dim GamerPic As Image
                                Profilepathstring = dirMU & profilename
                                Dim xPackage As New X360.STFS.STFSPackage(Profilepathstring.ToString, Nothing)
                                Dim a As X360.STFS.ProfilePackage = New X360.STFS.ProfilePackage(xPackage)

                                GamerPic = a.Header.PackageImage

                                gamertag = a.UserFile.GetGamertag()
                                SelectProfile2.ListView1.Items.Add(gamertag, count)
                                SelectProfile2.ListView1.Items(count).SubItems.Add(remotedirmu & profilename)

                                imageListLarge.Images.Add(GamerPic)

                                a.CloseIO()

                                ftp.ChangeRemoteDir(mustring & "Content/")

                                'GetHddProfileList(i)
                                count += 1
                            End If
                        End If

                    Next

                End If
            End If
        End If
        'mucount += 1
        'Loop
        mustring = "memunit0"
        success = ftp.ChangeRemoteDir(mustring & "Content/")

        If success = True Then
            Dim nu As Long
            nu = ftp.NumFilesAndDirs
            If (n < 0) Then
                'MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            Else

                If (nu > 0) Then
                    For i = 0 To nu - 1
                        If ftp.GetFilename(i).StartsWith("E") Then

                            Dim profilename As String = ftp.GetFilename(i)


                            Dim dirMU As String = Application.StartupPath & "\FTP\memunit0\" & ftp.GetFilename(i) & "\FFFE07D1\00010000\"
                            Dim remotedirmu As String = mustring & "Content/" & ftp.GetFilename(i) & "/FFFE07D1/00010000/" ' & ftp.GetFilename(i)

                            success = ftp.ChangeRemoteDir(remotedirmu & "/" & profilename)
                            If (success <> True) Then
                            Else

                                My.Computer.FileSystem.CreateDirectory(dirMU)
                                ftp.ChangeRemoteDir(remotedirmu)
                                success = ftp.DownloadTree(dirMU)

                                If (success <> True) Then
                                    MessageBox.Show(ftp.LastErrorText)
                                    'Exit For
                                End If

                                Dim gamertag As String
                                Dim GamerPic As Image
                                Profilepathstring = dirMU & profilename
                                Dim xPackage As New X360.STFS.STFSPackage(Profilepathstring.ToString, Nothing)
                                Dim a As X360.STFS.ProfilePackage = New X360.STFS.ProfilePackage(xPackage)

                                GamerPic = a.Header.PackageImage

                                gamertag = a.UserFile.GetGamertag()
                                SelectProfile2.ListView1.Items.Add(gamertag, count)
                                SelectProfile2.ListView1.Items(count).SubItems.Add(remotedirmu & profilename)

                                imageListLarge.Images.Add(GamerPic)

                                a.CloseIO()

                                ftp.ChangeRemoteDir(mustring & "Content/")

                                'GetHddProfileList(i)
                                count += 1
                            End If
                        End If

                    Next

                End If
            End If
        End If

        mustring = "memunit1"
        success = ftp.ChangeRemoteDir(mustring & "Content/")

        If success = True Then
            Dim nu As Long
            nu = ftp.NumFilesAndDirs
            If (n < 0) Then
                'MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            Else

                If (nu > 0) Then
                    For i = 0 To nu - 1
                        If ftp.GetFilename(i).StartsWith("E") Then

                            Dim profilename As String = ftp.GetFilename(i)


                            Dim dirMU As String = Application.StartupPath & "\FTP\memunit1\" & ftp.GetFilename(i) & "\FFFE07D1\00010000\"
                            Dim remotedirmu As String = mustring & "Content/" & ftp.GetFilename(i) & "/FFFE07D1/00010000/" ' & ftp.GetFilename(i)

                            success = ftp.ChangeRemoteDir(remotedirmu & "/" & profilename)
                            If (success <> True) Then
                            Else

                                My.Computer.FileSystem.CreateDirectory(dirMU)
                                ftp.ChangeRemoteDir(remotedirmu)
                                success = ftp.DownloadTree(dirMU)

                                If (success <> True) Then
                                    MessageBox.Show(ftp.LastErrorText)
                                    'Exit For
                                End If

                                Dim gamertag As String
                                Dim GamerPic As Image
                                Profilepathstring = dirMU & profilename
                                Dim xPackage As New X360.STFS.STFSPackage(Profilepathstring.ToString, Nothing)
                                Dim a As X360.STFS.ProfilePackage = New X360.STFS.ProfilePackage(xPackage)

                                GamerPic = a.Header.PackageImage

                                gamertag = a.UserFile.GetGamertag()
                                SelectProfile2.ListView1.Items.Add(gamertag, count)
                                SelectProfile2.ListView1.Items(count).SubItems.Add(remotedirmu & profilename)

                                imageListLarge.Images.Add(GamerPic)

                                a.CloseIO()

                                ftp.ChangeRemoteDir(mustring & "Content/")

                                'GetHddProfileList(i)
                                count += 1
                            End If
                        End If

                    Next

                End If
            End If
        End If

        imageListLarge.ImageSize = New Drawing.Size(64, 64)
        SelectProfile2.ListView1.LargeImageList = imageListLarge
        SelectProfile2.ShowDialog()
        'End If
    End Sub

    Friend Sub TransferProfiles()
        'Dim i As Integer

        'FTPProgress.showdialog()

        Dim n As Long
        n = ftp.NumFilesAndDirs
        ftp.ChangeRemoteDir(pathname)
        Dim profilepath As String = Application.StartupPath & "\FTP" & Replace(pathname, "Content/", "")
        'Dim local As String = Application.StartupPath & "/temp/bgmpacklocal.bin"
        'success = ftp.ChangeRemoteDir("/Usb0/Games/Ace Combat 6/")
        'ftp2.EnableEvents = True

        'If (success <> True) Then
        '    MessageBox.Show(ftp.LastErrorText)
        '    Exit Sub
        'End If

        ''FTPProgress.Show()
        'success = ftp.GetFile(remo)



        'Do Until ftp.AsyncFinished = True
        '    FTPProgress.SpeedLabel.Text = ftp.AsyncBytesReceived
        'Loop

        success = ftp.DownloadTree(profilepath)

        If (success <> True) Then
            MessageBox.Show(ftp.LastErrorText)
            Exit Sub
        End If

        'ftp.Disconnect()
        GetFiles.FolderPath = profilepath.Replace("/", "\")
        profilepath.TrimStart("\")
        GetSaves()
        If Main.SavesListView.Items.Count <> -1 Then
            Main.BackupsCheckBox.Enabled = True
            Main.UnflagButton.Enabled = True
            Main.ResignCheckBox.Enabled = True
            Main.RestoreBackupsToolStripMenuItem.Enabled = True
        End If

    End Sub

    Friend Sub FTPUpload()

        ftp.Hostname = HostName
        ftp.Username = UserName
        ftp.Password = Password
        ftp.Passive = Passive
        ftp.ConnectTimeout = 10
        ftp.KeepSessionLog = True

        If HostName = "" Or UserName = "" Or Password = "" Then
            MessageBox.Show("Please set the FTP options via the settings menu first", "error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        success = ftp.UnlockComponent(Main.ChilkatKey)
        If (Not success) Then
            MessageBox.Show(ftp.LastErrorText)
            Exit Sub
        End If


        If My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\FTP\Hdd1") Then

            success = ftp.Connect()
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.ChangeRemoteDir("\hdd1\content")
            If (success <> True) Then
                MessageBox.Show("Could not locate \Hdd1\content on the Xbox 360")
                Exit Sub
            End If

            success = ftp.PutTree(Application.StartupPath & "\FTP\Hdd1")
            If (success <> True) Then
                MessageBox.Show("Path does not exist on the Xbox 360")
                Exit Sub
            End If
        ElseIf My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\FTP\OnBoardMU") Then

            success = ftp.Connect()
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.ChangeRemoteDir("\OnBoardMU\content")
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.PutTree(Application.StartupPath & "\FTP\OnBoardMU")
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

        ElseIf My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\FTP\memunit0") Then

            success = ftp.Connect()
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.ChangeRemoteDir("\memunit0\content")
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.PutTree(Application.StartupPath & "\FTP\memunit0")
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If
        ElseIf My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\FTP\memunit1") Then

            success = ftp.Connect()
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.ChangeRemoteDir("\memunit1\content")
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If

            success = ftp.PutTree(Application.StartupPath & "\FTP\memunit1")
            If (success <> True) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If
        End If
        MessageBox.Show("Upload completed successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Friend Sub Test()
        ftp.Hostname = HostName
        ftp.Username = UserName
        ftp.Password = Password
        ftp.Passive = Passive
        ftp.ConnectTimeout = 10
        ftp.KeepSessionLog = True

        success = ftp.UnlockComponent("Anything for 30-day trial")
        If (Not success) Then
            MessageBox.Show(ftp.LastErrorText)
            Exit Sub
        End If

        success = ftp.Connect()

        If success <> True Then
            MsgBox(ftp.LastErrorText)
        End If

        success = ftp.ChangeRemoteDir("/")

        If success <> True Then
            MsgBox(ftp.LastErrorText)
        End If

        Dim i As Integer = 0
        Dim n As Integer
        If success = True Then


            n = ftp.NumFilesAndDirs
            If (n < 0) Then
                MessageBox.Show(ftp.LastErrorText)
                Exit Sub
            End If
            'Dim temp As String

            ftp.DownloadTree("c:\temp\")
            'ftp.GetTextDirListing("C:\temp.text")
            Dim count As Integer = 0
            'If (n > 0) Then
            '    For i = 0 To n - 1



            '        MsgBox(ftp.GetFilename(i))


            '    Next
            'End If
        End If
    End Sub
End Module
